package cn.fintecher.pangolin.service.business.service;

import cn.fintecher.pangolin.common.exception.BadRequestException;
import cn.fintecher.pangolin.common.model.UserModel;
import cn.fintecher.pangolin.common.utils.ZWDateUtil;
import cn.fintecher.pangolin.entity.elastic.Warning;
import cn.fintecher.pangolin.entity.mysql.CaseInfo;
import cn.fintecher.pangolin.service.business.model.request.WarningRequest;
import cn.fintecher.pangolin.service.business.model.response.FindWarningResponse;
import cn.fintecher.pangolin.service.business.repository.elasticsearch.WarningElastic;
import cn.fintecher.pangolin.service.business.repository.jparepository.CaseInfoRepository;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @Auther: xiaqun
 * @Description:
 * @Date: 15:16 2019/10/12
 */

@Service("warningService")
public class WarningService {
    @Autowired
    private BaseService baseService;

    @Autowired
    private WarningElastic warningElastic;

    @Autowired
    private CaseInfoRepository caseInfoRepository;

    @Autowired
    private ModelMapper modelMapper;

    /**
     * 添加警告
     */
    public void addWarning(WarningRequest request, String token) {
        UserModel userModel = baseService.getUserByToken(token);
        CaseInfo caseInfo = caseInfoRepository.findById(request.getCaseId()).orElseThrow(() -> new BadRequestException(null, "caseInfo", "case.is.null"));
        Warning warning = new Warning();
        warning.setContent(request.getContent()); //警告内容
        warning.setCardNo(caseInfo.getCardNo()); //卡号
        warning.setCaseNumber(caseInfo.getCaseNumber()); //案件编号
        warning.setCaseId(caseInfo.getId()); //案件ID
        warning.setCertificateNo(caseInfo.getCertificateNo()); //证件号
        warning.setId(UUID.randomUUID().toString().replaceAll("-", "")); //主键ID
        warning.setPersonalName(caseInfo.getPersonalName()); //客户姓名
        warning.setPrincipalId(caseInfo.getPrincipalId()); //委托方ID
        warning.setPrincipalName(caseInfo.getPrincipalName()); //委托方名称
        warning.process(userModel.getId(), userModel.getRealName(), ZWDateUtil.getNowDateTime());
        warningElastic.save(warning);
    }

    /**
     * 修改警告
     */
    public void modifyWarning(WarningRequest request, String token) {
        UserModel userModel = baseService.getUserByToken(token);
        Warning warning = warningElastic.findById(request.getId()).orElseThrow(() -> new BadRequestException(null, "warning", "warning.is.null"));
        warning.setContent(request.getContent());
        warning.process(userModel.getId(), userModel.getRealName(), ZWDateUtil.getNowDateTime());
        warningElastic.save(warning);
    }

    /**
     * 删除警告
     */
    public void deleteWarning(String id) {
        warningElastic.deleteById(id);
    }

    /**
     * 查询警告
     */
    public Page<FindWarningResponse> findAllWarning(String caseId, Pageable pageable) {
        BoolQueryBuilder builder = new BoolQueryBuilder();
        builder.must(QueryBuilders.matchPhraseQuery("caseId.keyword", caseId));
        SortBuilder sortBuilder = SortBuilders.fieldSort("operateTime").unmappedType("date").order(SortOrder.DESC);
        SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).withSort(sortBuilder).withPageable(pageable).build();
        Page<Warning> page = warningElastic.search(searchQuery);
        return page.map(e -> modelMapper.map(e, FindWarningResponse.class));
    }
}
